package cn.edu.scau.controller;

import java.util.List;

import cn.edu.scau.entity.Customer;
import cn.edu.scau.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.dao.NursingPlanMapper;
import cn.edu.scau.entity.NursingPlan;
import cn.edu.scau.entity.NursingPlanShow;
import cn.edu.scau.service.NursingPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "护理计划信息控制器")
@RestController
@RequestMapping("nursingPlan")
public class NursingPlanController {
	@Autowired
	private NursingPlanService nursingPlanService;
	@Autowired
	private CustomerService customerService;
	/**
	  * 查询所有护理计划信息 
	 * @return
	 */
	@ApiOperation(value = "返回所有护理计划信息")
	@GetMapping(value = "/list")
	public ResultBean list(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<NursingPlanShow> nursingContents=nursingPlanService.selectAll();
		PageInfo<NursingPlanShow> pageInfo = new PageInfo<NursingPlanShow>(nursingContents,pageSize);
		ResultBean resultBean =new ResultBean(200,true,"查询所有护理计划信息成功~~~",pageInfo);
		return resultBean;
	}
	


	
	
	/**
	 * 新增护理计划
	 * @param
	 * @param bindingResult
	 * @return
	 */
	@PostMapping(value = "/insert")
	public ResultBean insert(@Validated NursingPlan nursingPlan,BindingResult bindingResult)  {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}
		Customer customer = customerService.selectByPrimaryKey(nursingPlan.getCustomerid());
		if(customer!=null&&customer.getCustomeruseflag()) {
			boolean flag = nursingPlanService.insert(nursingPlan);
			if (flag) {
				resultBean = new ResultBean(200, true, "新增护理计划信息成功~~~\n", null);
			} else {
				resultBean = new ResultBean(5005, false, "新增护理计划信息失败", null);
			}
		}else
		{
			resultBean = new ResultBean(5005, false, "没有该客户", null);
		}
		return resultBean;
	}

	/**
	  * 根据编号查询护理计划信息
	 * @param
	 * @return
	 */
	@ApiOperation(value = "根据护理计划编号查询信息")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(paramType = "path",name = "customerid",value = "顾客编号",required = true,dataType = "int"),
			@ApiImplicitParam(paramType = "path",name = "nursingcontentid",value = "护理内容编号",required = true,dataType = "int"),
			@ApiImplicitParam(paramType = "path",name = "nursinglevelid",value = "护理等级编号",required = true,dataType = "int")
	})
	@GetMapping(value = "/{customerid}/{nursingcontentid}/{nursinglevelid}")
	public ResultBean detail(
			@PathVariable("customerid")Integer customerid,
			@PathVariable("nursingcontentid")Integer nursingcontentid,
			@PathVariable("nursinglevelid")Integer nursinglevelid) {
		NursingPlan nursingPlan = nursingPlanService.selectById(customerid, nursingcontentid, nursinglevelid);
		ResultBean resultBean=new ResultBean(200,true,"查询成功~~~",nursingPlan);
		return resultBean;
	}
	

	/**
	  * 修改护理计划信息
	 * @param
	 * @param
	 * @param bindingResult
	 * @return
	 */
	@PutMapping(value = "/update/{customerid}/{nursingcontentid}/{nursinglevelid}")
	public ResultBean update(
			@PathVariable("customerid")Integer customerid,
			@PathVariable("nursingcontentid")Integer nursingcontentid,
			@PathVariable("nursinglevelid")Integer nursinglevelid,
			@Validated NursingPlan nursingPlan,BindingResult bindingResult) {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}
		Customer customer = customerService.selectByPrimaryKey(nursingPlan.getCustomerid());
		if(customer!=null&&customer.getCustomeruseflag()) {
		boolean flag = nursingPlanService.update(nursingPlan);
		if(flag) {
			resultBean = new ResultBean(200,true,"修改护理计划成功~~~\n",null);
		}else {
			resultBean = new ResultBean(5005,false,"修改护理计划失败",null);
		}
		}else
		{
			resultBean = new ResultBean(5005, false, "没有该客户", null);
		}
		return resultBean;
	}
	

	/**
	  * 根据编号删除护理计划信息
	 * @param
	 * @return
	 */
	@DeleteMapping(value = "/delete/{customerid}/{nursingcontentid}/{nursinglevelid}")
	public ResultBean delete(
			@PathVariable("customerid")Integer customerid,
			@PathVariable("nursingcontentid")Integer nursingcontentid,
			@PathVariable("nursinglevelid")Integer nursinglevelid) {
		boolean flag = nursingPlanService.delete(customerid, nursingcontentid, nursinglevelid);
		ResultBean resultBean =null;
		if(flag) {
			resultBean = new ResultBean(200,true,"删除护理计划成功~~~\n删除护理计划编号为"+nursingcontentid,null);
		}else {
			resultBean = new ResultBean(5005,false,"删除护理计划失败",null);
		}
		return resultBean;
	}
}
